﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;

namespace RestService.Processing
{
    class Common
    {
        public static string ConnectionString
        {
            get
            {
                return @"SERVER = .; DATABASE = RadioMapDBNew; UID= sa; PWD=sqlP2ssw0rd";
            }
        }

        public static string DateVNToString(DateTime dateTime)
        {
            try
            {
                return dateTime.Day.ToString() + "/" + dateTime.Month.ToString() + "/" + dateTime.Year.ToString();
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string DateTimeVNToString(DateTime dateTime)
        {
            try
            {
                return dateTime.Day.ToString() + "/" + dateTime.Month.ToString() + "/" + dateTime.Year.ToString() + " " +
                    dateTime.Hour.ToString() + ":" + dateTime.Minute.ToString() + ":" + dateTime.Second.ToString();
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string DateTimeSQLToString(DateTime dateTime)
        {
            try
            {
                return dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + " " +
                    dateTime.Hour.ToString() + ":" + dateTime.Minute.ToString() + ":" + dateTime.Second.ToString();
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }

    public class ReferencePoint
    {
        public string MAC = "";
        public double ToaDoX = 0;
        public double ToaDoY = 0;
        public double RSS_AP1 = 0;
        public double RSS_AP2 = 0;
        public double RSS_AP3 = 0;
        public double RSS_AP4 = 0;
        public double RSS_AP5 = 0;
        public double RSS_AP6 = 0;
        public double RSS_AP7 = 0;
        public double RSS_AP8 = 0;
        public double RSS_AP9 = 0;
        public double RSS_AP10 = 0;
        //Mac tương ứng với AP phía trên
        public string AP1_MAC = "";
        public string AP2_MAC = "";
        public string AP3_MAC = "";
        public string AP4_MAC = "";
        public string AP5_MAC = "";
        public string AP6_MAC = "";
        public string AP7_MAC = "";
        public string AP8_MAC = "";
        public string AP9_MAC = "";
        public string AP10_MAC = "";

        public double EuclidDistance = 0;

        public static PointLBS PositionAVG(List<PointLBS> lsKNN)
        {
            PointLBS p = new PointLBS();
            p.ToaDoX = lsKNN.Average(x => x.ToaDoX);
            p.ToaDoY = lsKNN.Average(x => x.ToaDoY);

            return p;
        }
    }

    public  class ListAPInfo
    {
        public static List<APInfo> SortByRSS(List<APInfo> listAPInfo, bool asc)
        {
            List<APInfo> result = new List<APInfo>();
            if (asc)
                result = listAPInfo.OrderBy(x => x.RSS).ToList(); 
            else
                result = listAPInfo.OrderByDescending(x => x.RSS).ToList(); 

            return result;
        }

        public static List<APInfo> SortAndGetTopByRSS(List<APInfo> listAPInfo, bool asc, int top)
        {
            List<APInfo> result = new List<APInfo>();
            if (asc)
                result = listAPInfo.OrderBy(x => x.RSS).Take(top).ToList();
            else
                result = listAPInfo.OrderByDescending(x => x.RSS).Take(top).ToList();

            return result;
        }

        public static bool CheckAPExistedInPoint(string X, string Y, string MAC)
        {
            SqlConnection con = new SqlConnection(Common.ConnectionString);
            try
            {
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = new SqlDataAdapter();

                cmd.CommandText = string.Format(" SELECT * \n" +
                                                "FROM Signal A INNER JOIN Point B ON A.PID = B.ID \n" +
                                                "	INNER JOIN Ap C ON A.AID = C.ID \n" +
                                                "WHERE X= {0} AND Y = {1} AND MAC = '{2}'", 
                                                X, Y, MAC.Trim());

                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 1) return true;
                else return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
           
        }

    }
}
